package com.ytz.mq.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 12/29  13:47
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.66.100");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("ytz");
        connectionFactory.setPassword("ytz");
        connectionFactory.setVirtualHost("/");
        // 2.创建连接
        Connection connection = connectionFactory.newConnection();
        // 3.建立信道
        Channel channel = connection.createChannel();
        /**
         * 参数1：交换机名
         * 参数2：交换机类型
         * 参数3：交换机持久化
         */
        channel.exchangeDeclare("exchange_fanout", BuiltinExchangeType.FANOUT, true);
        channel.queueDeclare("SEND_MAIL2", true, false, false, null);
        channel.queueDeclare("SEND_MESSAGE2", true, false, false, null);
        channel.queueDeclare("SEND_STATION2", true, false, false, null);

        channel.queueBind("SEND_MAIL2", "exchange_fanout", "import");
        channel.queueBind("SEND_MESSAGE2", "exchange_fanout", "import");
        channel.queueBind("SEND_STATION2", "exchange_fanout", "import");
        channel.queueBind("SEND_STATION2", "exchange_fanout", "normal");


        channel.basicPublish("exchange_fanout", "import", null, ("双十一活动").getBytes(StandardCharsets.UTF_8));

        channel.basicPublish("exchange_routing", "normal", null, "小型促销活动".getBytes());
        channel.close();
        connection.close();

    }
}